package com.woniu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.client.AccountClient;
import com.woniu.commons.exception.VenueRuntimeException;
import com.woniu.commons.exception.YogaException;
import com.woniu.commons.model.StateData;
import com.woniu.dao.YCoachEnterVenueMapper;
import com.woniu.dao.YVenueBaseMapper;
import com.woniu.dao.YVenueCoachMapper;
import com.woniu.model.dto.CoachMyVenueDto;
import com.woniu.model.dto.VenueCoachDto;
import com.woniu.model.dto.VenueLoadCoachDto;
import com.woniu.model.model.YCoach;
import com.woniu.model.model.YCoachEnterVenue;
import com.woniu.model.model.YVenueBase;
import com.woniu.model.model.YVenueCoach;
import com.woniu.model.param.PageVenueCoachParam;
import com.woniu.model.param.VenueLoadCoachParam;
import com.woniu.service.YVenueCoachService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author EdisonLiu
 * @since 2024年05月23日
 */
@Service
public class YVenueCoachServiceImpl extends ServiceImpl<YVenueCoachMapper, YVenueCoach> implements YVenueCoachService {

    @Resource
    private YVenueCoachMapper venueCoachMapper;
    @Resource
    private YCoachEnterVenueMapper coachEnterVenueMapper;
    @Resource
    private AccountClient accountClient;
    @Resource
    private YVenueBaseMapper venueBaseMapper;






    @Override
    @Transactional
    public Integer leaveVenueCoach(Integer coachId, Integer venueId) {
        YVenueCoach venueCoach = venueCoachMapper.selectOne(new QueryWrapper<YVenueCoach>().eq("coach_id", coachId).eq("venue_id", venueId).eq("coach_state", StateData.COACH_STATE_OK));
        if (venueCoach == null){
            throw new VenueRuntimeException(3007,"该教练此时未在该场馆就职");
        }
        venueCoach.setCoachState(StateData.COACH_STATE_LEAVE);

        int i = venueCoachMapper.updateById(venueCoach);

        int delete = coachEnterVenueMapper.delete(new QueryWrapper<YCoachEnterVenue>().eq("coach_id", coachId).eq("venue_id", venueId));

        if (i == 1 && delete == 1){
            return 1;
        }else {
            throw new VenueRuntimeException(3008,"离职失败");
        }


    }

    @Override
    public Page<YVenueCoach> pageVenueCoach(PageVenueCoachParam param) {
        param.setPageNum(param.getPageNum() == null?1: param.getPageNum());
        param.setPageSize(param.getPageSize() == null?5: param.getPageSize());
        Page<YVenueCoach> page = new Page<>(param.getPageNum(), param.getPageSize());

        QueryWrapper<YVenueCoach> wrapper = new QueryWrapper<>();
        wrapper.eq("venue_id",param.getVenueId());
        if (param.getCoachState()!=null){
            wrapper.eq("coach_state",param.getCoachState());
        }
        if (param.getCoachTypeId()!= null){
            wrapper.eq("coach_type_id",param.getCoachTypeId());
        }
        Page<YVenueCoach> yVenueCoachPage = venueCoachMapper.selectPage(page, wrapper);
        return yVenueCoachPage;
    }

    /**
     * 根据场馆id去查询该场馆内所有的教练
     * @param venueId
     * @return
     */
    @Override
    public List<VenueCoachDto> loadListVenueCoach(Integer venueId) throws YogaException {
        QueryWrapper<YVenueCoach> wrapper = new QueryWrapper<>();
        wrapper.eq("venue_id",venueId).eq("coach_state",1);
        List<YVenueCoach> venueCoaches = baseMapper.selectList(wrapper);

        ArrayList<VenueCoachDto> venueCoachDtos = new ArrayList<>();
        for (YVenueCoach vc:venueCoaches){
            VenueCoachDto venueCoachDto = BeanUtil.toBean(vc, VenueCoachDto.class);
            YCoach data = accountClient.byId(vc.getCoachId()).getData();
            venueCoachDto.setCoachName(data.getCoachName());
            venueCoachDtos.add(venueCoachDto);
        }

        return venueCoachDtos;
    }

    /**
     * 场馆端分页查询场馆里面已经入驻的教练
     *
     * @param param 分页信息
     * @return
     */
    @Override
    public Page<VenueLoadCoachDto> loadPageVenueCoach(VenueLoadCoachParam param) throws YogaException {
        if (param.getPageNum() == null) {
            param.setPageNum(1);
        }
        if (param.getPageSize() == null) {
            param.setPageSize(5);
        }
        if (param.getVenueId()==null){
            throw new YogaException(500,"还未登录场馆");
        }
        //拼接条件
        QueryWrapper<YVenueCoach> wrapper = new QueryWrapper<YVenueCoach>();
        wrapper.eq("coach_state",1);
        wrapper.eq("venue_id",param.getVenueId());
        Page<YVenueCoach> venueCoachPage = venueCoachMapper.selectPage(new Page<YVenueCoach>(param.getPageNum(), param.getPageSize()), wrapper);

        Page<VenueLoadCoachDto> page = new Page<>();
        page.setCurrent(venueCoachPage.getCurrent());
        page.setOptimizeCountSql(venueCoachPage.optimizeCountSql());
        page.setPages(venueCoachPage.getPages());
        page.setSearchCount(venueCoachPage.searchCount());
        page.setSize(venueCoachPage.getSize());
        page.setTotal(venueCoachPage.getTotal());

        ArrayList<VenueLoadCoachDto> dto = new ArrayList<>();

        for (YVenueCoach venueCoach:venueCoachPage.getRecords()){
            VenueLoadCoachDto pageDto = BeanUtil.toBean(venueCoach, VenueLoadCoachDto.class);

            //通过教练ID查出教练名字赋值给dto
            String coachName = accountClient.byId(venueCoach.getCoachId()).getData().getCoachName();
            if (coachName == null){
                throw new YogaException(500,"请先招募教练");
            }
            pageDto.setCoachName(coachName);

            //通过场馆ID查出场馆名字
            YVenueBase yVenueBase = venueBaseMapper.selectById(venueCoach.getVenueId());
            if (yVenueBase == null){
                throw new YogaException(500,"场馆不存在");
            }
            pageDto.setVenueName(yVenueBase.getVenueName());

            dto.add(pageDto);
        }
        page.setRecords(dto);
        return page;

    }


    /**
     * 根据教练id去查询该教练所在的场馆详细信息
     * @param coachId
     * @return
     */
    @Override
    public CoachMyVenueDto loadCoachMyVenue(Integer coachId) throws YogaException {
        //关系表对象
        QueryWrapper<YVenueCoach> wrapper = new QueryWrapper<>();
        wrapper.eq("coach_id",coachId);
        YVenueCoach venueCoach = venueCoachMapper.selectOne(wrapper);
        if (venueCoach==null){
            throw new YogaException(500,"还没入驻场馆，请尽快入驻");
        }
        if (venueCoach.getCoachState()==2){
            throw new YogaException(500,"教练状态有误");
        }
        if (venueCoach.getOutDate()!=null){
            throw new YogaException(500,"教练已离职");
        }

        //场馆详细信息
        YVenueBase venueBase = venueBaseMapper.selectById(venueCoach.getVenueId());

        //查教练类型状态名
        String coachTypeName = accountClient.CoachTypeById(venueCoach.getCoachTypeId()).getData().getCoachTypeName();

        CoachMyVenueDto dto = new CoachMyVenueDto();
        dto.setCoachId(venueCoach.getCoachId());
        dto.setVenueId(venueCoach.getVenueId());
        dto.setCoachTypeId(venueCoach.getCoachTypeId());
        dto.setCoachTyName(coachTypeName);
        dto.setCoachPhone(venueCoach.getCoachPhone());
        dto.setCoachState(venueCoach.getCoachState());
        dto.setCardImage(venueCoach.getCardImage());
        dto.setJoinDate(venueCoach.getJoinDate());
        dto.setOutDate(venueCoach.getOutDate());
        dto.setVenueName(venueBase.getVenueName());
        dto.setVenueImage(venueBase.getVenueImage());
        dto.setVenueLicense(venueBase.getVenueLicense());
        dto.setVenueDetails(venueBase.getVenueDetails());
        dto.setBusinessState(venueBase.getBusinessState());
        dto.setVenueState(venueBase.getVenueState());
        dto.setVenueAddress(venueBase.getVenueAddress());
        dto.setBusinessStartTime(venueBase.getBusinessStartTime());
        dto.setBusinessEndTime(venueBase.getBusinessEndTime());
        dto.setLongitude(venueBase.getLongitude());
        dto.setLatitude(venueBase.getLatitude());
        dto.setArea(venueBase.getArea());
        dto.setCreateTime(venueBase.getCreateTime());
        dto.setClassroomNum(venueBase.getClassroomNum());
        dto.setScore(venueBase.getScore());


        return dto;
    }

    /**
     * 根据场馆Id去查询该场馆内所有的教练
     * @param venueId
     * @return
     */
    @Override
    public List<VenueCoachDto> VenueByIdCoach(Integer venueId) throws YogaException {
        QueryWrapper<YVenueCoach> wrapper = new QueryWrapper<>();
        wrapper.eq("venue_id",venueId).eq("coach_state",1);
        List<YVenueCoach> venueCoaches = baseMapper.selectList(wrapper);

        ArrayList<VenueCoachDto> venueCoachDtos = new ArrayList<>();
        for (YVenueCoach vc:venueCoaches){
            VenueCoachDto venueCoachDto = BeanUtil.toBean(vc, VenueCoachDto.class);
            YCoach data = accountClient.byId(vc.getCoachId()).getData();
            venueCoachDto.setCoachName(data.getCoachName());
            venueCoachDtos.add(venueCoachDto);
        }

        return venueCoachDtos;
    }


}
